package com.yum.Iterator.Set;

import com.yum.Iterator.Collection.YAbstractCollection;
import com.yum.Iterator.Collection.YCollection;
import com.yum.Iterator.YIterator;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Objects;

public abstract class YAbstractSet<E> extends YAbstractCollection<E> {

    protected YAbstractSet() {
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == this){
            return true;
        }
        if(!(obj instanceof YSet)){
            return false;
        }
        YCollection<?> Yc = (YCollection<?>) obj;
        return Yc.size() == size();
    }

    @Override
    public int hashCode() {
        int h = 0;
        for (E obj : this) {
            if (obj != null)
                h += obj.hashCode();
        }
        return h;
    }

    @Override
    public boolean removeAll(YCollection<?> Yc) {
        Objects.requireNonNull(Yc);
        boolean modified = false;
        if(size() > Yc.size()){
            for (Object o : Yc) modified |= remove(o);
        }else{
            for(Iterator<?> i = iterator();i.hasNext();){
                if(Yc.contains(i.next())){
                    i.remove();
                    modified = true;
                }
            }
        }
        return modified;
    }
}
